package com.example.filecollectionsystemdemo1.controller;

import cn.hutool.core.util.StrUtil;
import com.example.filecollectionsystemdemo1.common.AuthAccess;
import com.example.filecollectionsystemdemo1.common.Result;
import com.example.filecollectionsystemdemo1.entity.Users;
import com.example.filecollectionsystemdemo1.service.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;



@Slf4j
@RestController
public class WebController {

    @Resource
    UsersService usersService;

    @GetMapping("/")
    public Result hello() {
        return Result.success("success");
    }


    @PostMapping("/login")
    public Result login(@RequestBody Users users) {
        if (StrUtil.isBlank(users.getUsername()) || StrUtil.isBlank(users.getPassword())) {
            return Result.error("数据输入不合法");
        }
        users = usersService.login(users);
        return Result.success(users);
    }

    @AuthAccess
    @PostMapping("/register")
    public Result register(@RequestBody Users users) {
        if (StrUtil.isBlank(users.getUsername()) || StrUtil.isBlank(users.getPassword())||StrUtil.isBlank(users.getUsername()) ) {
            return Result.error("数据输入不合法");
        }
        if (users.getUsername().length() > 10 || users.getPassword().length() > 20) {
            return Result.error("数据输入不合法");
        }
        users = usersService.register(users);
        return Result.success(users);
    }
    /**
     *  Controller重置密码
     */
    @AuthAccess
    @PutMapping("/password")
    public Result password(@RequestBody Users users) {
        if (StrUtil.isBlank(users.getUsername()) || StrUtil.isBlank(users.getPhone())) {
            return Result.error("数据输入不合法");
        }
        usersService.resetPassword(users);
        return Result.success();
    }

}